package com.hmall.search.web.controller;

import com.hmall.domain.doc.ItemDoc;
import com.hmall.domain.dto.ItemSearchDto;
import com.hmall.domain.dto.PageDTO;
import com.hmall.search.service.ItemSearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 商品es web层控制器
 *
 * @Author XiaoJiangShi
 * @Date 2022/8/10 16:32
 * @Blog blog.XiaoJiangShi.net/996.mba
 * @Description:
 */
@Slf4j
@RestController("/search")
public class ItemSearchController {
    @Autowired
    private ItemSearchService searchService;

    /**
     * 根据搜索框输入，自动补全
     *
     * @param prefix 需补全的数据
     * @return suggestion
     */
    @GetMapping("/suggestion")
    public List<String> suggestionByKey(@RequestParam("key") String prefix) {
        log.info("需要不全的信息为[{}]", prefix);

        return searchService.suggestionByKey(prefix);
    }

    /**
     * 根据条件搜索并聚合数据
     *
     * @param searchDto 搜索的条件
     * @return 聚合数据
     */
    @PostMapping("/filters")
    public Map<String, List<String>> searchByConditionForPoly(@RequestBody ItemSearchDto searchDto) {
        log.info("条件搜索的数据为：[{}]", searchDto.toString());
        return searchService.polyForCondition(searchDto);
    }

    /**
     * 对商品进行多条件查询
     * @param searchDto 条件对象
     * @return 返回值
     */
    @PostMapping("/list")
    public PageDTO<ItemDoc> list(@RequestBody ItemSearchDto searchDto) {
        log.info("多条件查询商品数据：[{}]", searchDto.toString());

        return searchService.listByCondition(searchDto);
    }
}

